<?php


namespace app\cms\oauth\controller;


use app\community\boot\oauth2\oauthBoot;
use iflow\Container\implement\annotation\tools\data\Inject;
use iflow\Response;
use iflow\Router\Controller;
use iflow\Router\implement\Request\GetMapping;
use League\OAuth2\Server\Grant\ImplicitGrant;
use Psr\Http\Message\ResponseInterface;

#[Controller('%oauth2%/authorize')]
class authorizationImplicit
{

    #[Inject]
    public oauthBoot $oauthBoot;

    /**
     * 隐式授权
     * @return Response|ResponseInterface
     */
    #[GetMapping]
    public function implicit(): Response|ResponseInterface
    {
        try {
            $P1TH = new \DateInterval('PT1H');
            $this->oauthBoot
                -> enableGrantType(
                    new ImplicitGrant($P1TH),
                    false,
                    new \DateInterval('PT1H')
                )
                -> validateAuthorizationRequest()
                -> getAuthorizationRequest()
                -> setAuthorizationApproved(true);

            return $this->oauthBoot
                -> getOauthServer()
                -> completeAuthorizationRequest(
                    $this->oauthBoot -> getAuthorizationRequest(),
                    response() -> getResponsePsr7()
                );
        } catch (\Throwable $exception) {
            return message() -> server_error($exception -> getCode(), $exception -> getMessage());
        }
    }

}